Page({
  /**
   * 页面的初始数据
   */
  data: {
    bannerIdx: '0',
    load: true,
    page_index: 0,
    bannerImg: '',
    video_list: [],
    text: '\
        支持弹幕功能测试的视频:\n\
        《这个勇者明明超强却过分慎重》、\n\
        《你的名字》《鬼灭之刃》\n',
    itemList: [{
        id: 'movies',
        name: '#电影',
        img: 'http://navod.scse.com.cn/nn_cms/data/upimg/vod/0/a/1/1fc96b8bede678a8aaaf7f60d7648c30/67bf0d1bd9a3cd722dd2ac16bd896ce2.jpg'
      }, {
        id: 'KoreaJapan',
        name: '#日韩',
        img: 'http://navod.scse.com.cn/nn_cms/data/upimg/vod/f/3/5/5a509a9f19d4ed1735fffc6cd3126a0f/552d91bc846525dc4e5b32951cc19839.jpg'
      }, {
        id: 'animation',
        name: '#动漫',
        img: 'http://navod.scse.com.cn/nn_cms/data/upimg/vod/5/7/c/c9ce1307d38d81087951774b4f4477b5/33e048f39baed54b8ba7f073e599294f.jpg'
      },
      {
        id: 'variety',
        name: '#综艺',
        img: 'http://navod.scse.com.cn/nn_cms/data/upimg/vod/a/2/5/5f24d7651d8321f32cba9435a2bb3eaa/e50dc103e7134ce49a9b136dcf94a2b5.PNG'
      }, {
        id: 'TVseries',
        name: '#电视',
        img: 'http://navod.scse.com.cn/nn_cms/data/upimg/vod/f/b/8/84b806bc3de99c0cbbbfb384098e609f/f9078536d3d3cfc2400c9038942054a7.jpg'
      },
      {
        id: 'softmovies',
        name: '#体育',
        img: 'http://navod.scse.com.cn/nn_cms/data/upimg/vod/7/a/3/31d41c9dbe078a42aa72aed0c4256a67/1cb9dcd9073140af1c6a947ccd4bc6b4.jpg'
      }, {
        id: 'schoolmv',
        name: '#校园',
        img: 'http://navod.scse.com.cn/nn_cms/data/upimg/vod/9/4/5/5274b7a8fc8c086a442bbc3a57c9c599/378bdef6a3dd8ef09d28d964f959d6bb.jpg'
      }

    ],
    tapCount: 0
  },
  changeIndex: function(e) {
    //console.log(e)
    this.setData({
      bannerIdx: e.detail.current
    })
  },
  searchPage: function(e) {
    wx.navigateTo({
      url: '../search/search',
    })
  },
  menuPage: function(e) {
    wx.switchTab({
      url: '../menu/menu',
    })
  },
  //彩蛋-进入关于页面
  aboutPage: function(e) {
    var tap = this.data.tapCount
    if (tap < 1) {
      tap++
      this.setData({
        tapCount: tap
      })
    } else {
      wx.navigateTo({
        url: './about/about',
      })
    }
  },
  videoPage: function(e) {
    console.log(e)
    wx.navigateTo({
      url: '../video/video?id=' + e.currentTarget.dataset.val
    })
  },

// 获取推荐视频列表
  getVideoList: function(e) {
    var that = this
    wx: wx.request({
      url: 'http://navod.scse.com.cn/nn_cms/nn_cms_view/std/n3_a_xml.php',
      data: {
        nns_nxtype: 'd',
        nns_tag: 29,
        nns_func: 'search_media_assets_item',
        nns_media_assets_id: 'TVseries|movies|schoolmv|animation|variety|KoreaJapan|softmovies',
        nns_search_type: 'name_likechar',
        nns_search_key: '',
        nns_output_type: 'json',
        nns_page_size: 9,
        nns_page_index: that.data.page_index
      },
      method: 'GET',
      success: function(res) {
        if (res.data.data.item != undefined) {
          //console.log(res.data.data.item)
          var content = that.data.video_list.concat(res.data.data.item)
          //console.log('videoList=>')
          //console.log(res)
          that.setData({
            video_list: content,
            page_index: that.data.page_index + 1
          })
        }
      },
      fail: function(res) {
        wx.showModal({
          //title: '提示',
          content: '请检查网络状态后重试!',
          showCancel: false,
          confirmColor: '#d50000',
        })
      },
      complete: function(res) {
        that.setData({
          load: true
        })
      },
    })
  },
  // 获取展示封面
  getBannerImg: function() {
    var that = this
    var idx = Math.floor(Math.random() * 150)
    //console.log(idx)
    wx.request({
      url: 'http://navod.scse.com.cn/nn_cms/nn_cms_view/std/n3_a_xml.php',
      data: {
        nns_nxtype: 'd',
        nns_tag: 29,
        nns_func: 'search_media_assets_item',
        nns_media_assets_id: 'TVseries|movies|animation|variety|KoreaJapan',
        nns_search_type: 'name_likechar',
        nns_search_key: '',
        nns_output_type: 'json',
        nns_page_size: 1,
        nns_page_index: idx
      },
      method: 'GET',
      success: function(res) {
        //console.log('getRecommend')
        //console.log(res)
        that.setData({
          bannerImg: res.data.data.item['@attributes']
        })

      },
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      page_index: Math.floor(Math.random() * 30)
    })
    this.getVideoList()
    this.setData({
      tapCount: 0
    })
    this.getBannerImg()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.load) {
      this.setData({
        load: false
      })
      this.getVideoList()
    }

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})